package com.cuit.ocp.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cuit.ocp.bean.ReturnOrder;
import com.cuit.ocp.bean.Role;
import com.cuit.ocp.bean.Trader;
import com.cuit.ocp.dao.TraderDao;
import com.cuit.ocp.service.TraderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TraderServicelmpl implements TraderService {
    @Resource
    private TraderDao traderDao;

    @Override
    public boolean addNewTrader(Trader new_trader){
        if( traderDao.addNewTrader(new_trader) > 0 )
            return true;
        else
            return false;
    }

    @Override
    public boolean update(Trader trader_info){
        if(traderDao.update(trader_info) > 0 )
            return true;
        else
            return false;
    }

    @Override
    public boolean deleteById(int trader_id){
        if(traderDao.deleteById(trader_id) > 0)
            return true;
        else
            return false;
    }

    @Override
    public boolean deleteByIds(List<Integer> staff_ids){
        if(traderDao.deleteByIds(staff_ids) > 0)
            return true;
        else
            return false;
    }

    @Override
    public List<Trader> findById(int id){

        List<Trader> result = new ArrayList<>();
        result.add(traderDao.findById(id));
        return result;
    }

    @Override
    public List<Trader> findByName(String name){
        List<Trader> result = new ArrayList<>();
        result.add(traderDao.findByName(name));
        return result;
    }

    @Override
    public Map<Object, Object> findAll(int page_num){

        PageHelper.startPage(page_num,5);
        List<Trader> result = traderDao.findAll();
        PageInfo<Trader> pageInfo = new PageInfo<>(result);
        Map<Object, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("rows", pageInfo.getList());
        return map;
    }

    @Override
    public Trader jsonToTrader(String jsonString){
        Trader trader = new Trader();
        JSONObject json_info = JSON.parseObject(jsonString);
        System.out.println("新数据: " + json_info);
        trader.setTradername(json_info.getString("tradername"));
        trader.setPassword(json_info.getString("password"));
        trader.setLinkman(json_info.getString("linkman"));
        trader.setPs(json_info.getString("ps"));
        trader.setRealName(json_info.getString("realName"));
        JSONObject json_role = json_info.getJSONObject("role");
        System.out.println("用户名: " + json_info.getString("tradername"));
        System.out.println("用户密码: " + json_info.getString("password"));
        System.out.println("json_role: " + json_role);
        Role role = new Role();
        role.setRid(json_role.getInteger("rid"));
        trader.setRole(role);
        trader.setTid(json_info.getInteger("tid"));
        trader.setTraderMail(json_info.getString("traderMail"));
        trader.setTraderPhone(json_info.getString("traderPhone"));
        trader.setTraderStatus(json_info.getString("traderStatus"));
        System.out.println("用户名: " + trader.getTradername());
        System.out.println("用户密码: " + trader.getPassword());
        return trader;
    }
}
